package com.dev.module_ui_composable.components

import android.content.res.Configuration
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun TimerProgressIndicator(progress: Float, totalTime: Int) {
    val timerStatus by remember { mutableStateOf(false) }
    var elapsedTime by remember { mutableFloatStateOf(0f) }
    // 每个周期的时间(5分钟)
    var cycleTime by remember { mutableFloatStateOf(0f) }
    val coroutineScope = rememberCoroutineScope()

    // 每个周期的总持续时间(以秒为单位)(5分钟)
    val cycleDuration = 300f

    val cycleProgress by animateFloatAsState(
        targetValue = cycleTime / cycleDuration,
        animationSpec = tween(
            durationMillis = 300,
            easing = LinearEasing
        ),
        label = "timer_Progress"
    )

    LaunchedEffect(timerStatus) {
        if (timerStatus) {
            coroutineScope.launch {
                while (timerStatus) {
                    delay(100)
                    elapsedTime += 0.1f
                    cycleTime += 0.5f
                    if (cycleTime >= cycleDuration) {
                        // 每 5分钟后重置周期时间
                        cycleTime = 0f
                    }
                }
            }
        } else {
            elapsedTime = 0f
            cycleTime = 0f
        }
    }

    Box(
        modifier = Modifier
            .padding(16.dp)
            .size(220.dp)
            .aspectRatio(1f),
        contentAlignment = Alignment.Center
    ) {
        val stroke = with(LocalDensity.current) { 20.dp.toPx() }
        val textSize = with(LocalDensity.current) { 40.sp.toPx() }
        val progressAngle = progress * 360f
        val minutes = totalTime / 60
        val seconds = totalTime % 60

        Canvas(modifier = Modifier.size(220.dp)) {
            // 绘制背景圆
            drawArc(
                color = Color(0xFFE7EEFE),
                startAngle = 270f,
                sweepAngle = 360f,
                useCenter = false,
                topLeft = Offset(stroke / 2, stroke / 2),
                size = Size(size.width - stroke, size.height - stroke),
                style = Stroke(width = stroke)
            )

            val gradient = Brush.linearGradient(
                colors = listOf(Color(0xFF7298F5), Color(0xFF0D4DED)),
                start = Offset.Zero,
                end = Offset(size.width, size.height)
            )

            // 绘制进度弧线
            drawArc(
                brush = gradient,
                startAngle = 270f,
                sweepAngle = progressAngle,
                useCenter = false,
                topLeft = Offset(stroke / 2, stroke / 2),
                size = Size(size.width - stroke, size.height - stroke),
                style = Stroke(width = stroke, cap = StrokeCap.Round)
            )
        }

        // 中间绘制文字
        Text(
            text = "%02d:%02d".format(minutes, seconds),
            style = TextStyle(
                fontSize = with(LocalDensity.current) { textSize.toSp() },
                color = Color.Black
            )
        )
    }
}

@Composable
fun ProgressIndicator(
    modifier: Modifier = Modifier,
    progress: Float,
) {
    val animatedProgress by animateFloatAsState(
        targetValue = progress,
        animationSpec = tween(
            durationMillis = 1000,
            easing = FastOutSlowInEasing
        ),
        label = "progress"
    )

    Box(
        modifier = modifier
            .padding(15.dp)
            .size(220.dp)
            .aspectRatio(1f),
        contentAlignment = Alignment.Center
    ) {
        val stroke = with(LocalDensity.current) { 20.dp.toPx() }
        val textSize = with(LocalDensity.current) { 40.sp.toPx() }
        val progressAngle = animatedProgress * 360f

        Canvas(modifier = Modifier.fillMaxSize()) {
            // 绘制背景圆
            drawArc(
                color = Color(0xFFE7EEFE),
                startAngle = 270f,
                sweepAngle = 360f,
                useCenter = false,
                topLeft = Offset(stroke / 2, stroke / 2),
                size = Size(size.width - stroke, size.height - stroke),
                style = Stroke(width = stroke)
            )

            val gradient = Brush.linearGradient(
                colors = listOf(Color(0xFF49DBD0), Color(0xFF60EFB9)),
                start = Offset.Zero,
                end = Offset(size.width, size.height)
            )

            // 绘制进度弧线
            drawArc(
                brush = gradient,
                startAngle = 270f,
                sweepAngle = progressAngle,
                useCenter = false,
                topLeft = Offset(stroke / 2, stroke / 2),
                size = Size(size.width - stroke, size.height - stroke),
                style = Stroke(width = stroke, cap = StrokeCap.Round)
            )
        }

        // 中间绘制文字
        Text(
            text = "%.0f%%".format(animatedProgress * 100),
            style = TextStyle(
                fontSize = with(LocalDensity.current) { textSize.toSp() },
                color = Color.Black
            )
        )
    }
}

@Preview(
    name = "Light Mode",
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    locale = "zh-rCN",
    showBackground = true,
    device = "spec:width=1440px,height=3200px,dpi=560"
)
@Composable
private fun TimerProgressPreview() {
    TimerProgressIndicator(progress = 0.7f, totalTime = 30 * 60 + 33)
}

@Preview(
    name = "Light Mode",
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    locale = "zh-rCN",
    showBackground = true,
    device = "spec:width=1440px,height=3200px,dpi=560"
)
@Composable
private fun ProgressIndicatorPreview() {
    ProgressIndicator(progress = 0.5f)
}